package com.ly.rabbitmq.client;

import com.rabbitmq.client.BasicProperties;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import org.junit.jupiter.api.Test;


import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Classname Producer
 * @Description TODO
 * @Date 2020/11/19 15:31
 * @Author 冷心影翼
 */
public class Producer {

	@Test
	public void productMsg() throws IOException, TimeoutException {
		Connection connection = ConnectionUtil.getConnection();
		Channel channel = connection.createChannel();

		/**
		 *
		 * 参数1：队列名称     String queue
		 * 参数2：是否定义持久化队列     boolean durable
		 * 参数3：是否独占本次连接 boolean exclusive
		 * 参数4：是否在不使用的时候自动删除队列  boolean autoDelete
		 * 参数5：队列其它参数   Map<String, Object> arguments
		 */

		channel.queueDeclare("queue1", true, false, false, null);

		channel.queueBind("queue1","directExchange","direct");
//		channel.queueBind("queue2","directExchange","direct2");

		// 发送信息
		String message = "你好；小兔子！" ;
		/**
		 * 参数1：交换机名称，如果没有指定则使用默认Default Exchage   String exchange
		 * 参数2：路由key,简单模式可以传递队列名称   String routingKey
		 * 参数3：消息其它属性   BasicProperties props
		 * 参数4：消息内容     byte[] body
		 */
		channel.basicPublish("directExchange", "direct", null, message.getBytes());
//		channel.basicPublish("", "queue1", null, message.getBytes());
		System.out.println("已发送消息：" + message);


		// 关闭资源
		channel.close();
		connection.close();
	}


	@Test
	public void productByFanout() throws IOException, TimeoutException {
		Connection connection = ConnectionUtil.getConnection();
		Channel channel = connection.createChannel();

		String queue1 = "queue1";
		String queue2 = "queue2";
		channel.queueDeclare(queue1,true,false,false,null);
		channel.queueDeclare(queue2,true,false,false,null);

		String exchange = "amq.fanout";
		/*
			String exchange,
			BuiltinExchangeType type,
			boolean durable,
			boolean autoDelete,
			Map<String, Object> arguments
		 */
		channel.exchangeDeclare(exchange, BuiltinExchangeType.FANOUT,true,false,null);


		String message = "fanout by queue1" ;

		channel.queueBind(queue1,exchange,"");
		channel.queueBind(queue2,exchange,"");
		channel.basicPublish("amq.fanout","",null,message.getBytes());

	}

	@Test
	public void productByRoutingkey() throws IOException, TimeoutException {
		Connection connection = ConnectionUtil.getConnection();
		Channel channel = connection.createChannel();

		String exchangeName = "exchange.topic.log";
		channel.exchangeDeclare(exchangeName,BuiltinExchangeType.TOPIC,true,false,false,null);

		String queue1 = "queue1";
		String queue2 = "queue2";
		String queue3 = "queue3";
		String queue4 = "queue4";
		channel.queueDeclare(queue1,true,false,false,null);
		channel.queueDeclare(queue2,true,false,false,null);
		channel.queueDeclare(queue3,true,false,false,null);
		channel.queueDeclare(queue4,true,false,false,null);

		channel.queueBind(queue1,exchangeName,"#.warning");
		channel.queueBind(queue2,exchangeName,"#.error");
		channel.queueBind(queue3,exchangeName,"controller.#");
		channel.queueBind(queue4,exchangeName,"service.#");


		String se = "service of error";
		String sw = "service of warning";
		String cw = "controller of warning ";
		String ce = "controller of error ";
		channel.basicPublish(exchangeName,"service.warning",null,sw.getBytes());
		channel.basicPublish(exchangeName,"service.error",null,se.getBytes());
		channel.basicPublish(exchangeName,"controller.warning", null,cw.getBytes());
		channel.basicPublish(exchangeName,"controller.error",null,ce.getBytes());
	}
}
